var front = window.front = window.front || {util: {}, layout: {}, view: {}, ajax: {}, data: {}, component: {}, biz:{}};

front.util = (function () {
    return {
        /**
         * 判断是否为null、undifined、''、{};
         *
         * @param value
         * @returns {*}
         */
        isEmpty: function (value) {
            if (_.isNull(value) || _.isUndefined(value)) {
                return true;
            }
            else if (_.isString(value) && value == '') {
                return true;
            }
            return _.isObjectLike(value) && _.isEmpty(value);
        }
    }
})();

front.ajax = (function () {
    var self = {};
    var AJAX_DEFAULTS = {
        type: "POST",
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        statusCode: {
            404: function () {
                alert("404 page not found");
            }
        }
    }
    /***
     * 默认是：post并且是json
     * @param options
     * @private
     */
    var _ajax = function (options) {
        options.url = front.ajax.curl(options.url);
        return $.ajax($.extend({}, AJAX_DEFAULTS, options)).fail(function (jqXHR, textStatus, errorThrown) {
            front.ajax.alertErrMsg(jqXHR, textStatus, errorThrown)
        })
    }
    var _ajax_get = function (options) {
        $.extend(options, {type: "get"})
        var url = front.ajax.addUrlParam(options.url,'_t', new Date().getTime());
        options.url = url;
        return $.ajax($.extend({}, AJAX_DEFAULTS, options))
    }
    var _get = function (url, data) {
        url = front.ajax.addUrlParam(url,'_t', new Date().getTime());
        url = front.ajax.curl(url);
        return $.get(url, data).fail(function (jqXHR, textStatus, errorThrown) {
            front.ajax.alertErrMsg(jqXHR, textStatus, errorThrown)
        })
    }
    var _post = function (url, data) {
        url = front.ajax.curl(url);
        return $.post(url, data).fail(function (jqXHR, textStatus, errorThrown) {
            front.ajax.alertErrMsg(jqXHR, textStatus, errorThrown)
        })
    }

    $.extend(self,{
        fetch: _ajax,
        getFetch:_ajax_get,
        get:   _get,
        post:  _post
    });

    self.curl = function (url, mock) {
        var _ctx = window.ctx||'';
        url = _.trim(url);
        url = _.startsWith(url, '/') ? url : '/' + url;
        return mock ? _ctx + '/mock' + url : _ctx + url;
    }
    self.addUrlParam = function (href, name, value) {
        var currentUrl = href.split('#')[0];
        if (/\?/g.test(currentUrl)) {
            if (/name=[-\w]{4,25}/g.test(currentUrl)) {
                currentUrl = currentUrl.replace(/name=[-\w]{4,25}/g, name + "=" + value);
            } else {
                currentUrl += "&" + name + "=" + value;
            }
        } else {
            currentUrl += "?" + name + "=" + value;
        }
        if (href.split('#')[1]) {
            return currentUrl + '#' + href.split('#')[1];
        } else {
            return currentUrl;
        }
    }
    self.alertErrMsg = function (jqXHR, textStatus, errorThrown) {
        if (jqXHR.readyState == 0) {
            return
        }
        var xhr = "jqXHR=" + JSON.stringify(jqXHR) + "\n";
        var status = "textStatus=" + JSON.stringify(textStatus) + "\n";
        var thrown = "errorThrown=" + JSON.stringify(errorThrown) + "\n";
        var msg = 'ajax errs:\n' + xhr + status + thrown;
        layer.msg(msg);
    }
    self.ajaxLoad = function (options, okCallback, failCallback) {
        layer.load();
        return _ajax(options).fail(function (jqXHR, textStatus, errorThrown) {
            self.alertErrMsg(jqXHR, textStatus, errorThrown);
        }).always(function () {
            layer.closeAll('loading');
        }).done(function (result) {
            if ('ok' == result.status) {
                okCallback && okCallback(result)
            }
            else {
                failCallback ? failCallback(result) : layer.msg(result.message);
            }
        });
    }
    self.download = function (url, data, method) {
        if (url) {
            var inputs = '';
            if(data){
                data = typeof data == 'string' ? data : $.param(data);
                var inputs = '';
                $.each(data.split('&'), function () {
                    var pair = this.split('=');
                    inputs += '<input type="hidden" name="' + pair[0] + '" value="' + pair[1] + '" />';
                });
            }
            layer.load();
            $('<form action="' + url + '" method="' + (method || 'post') + '">' + inputs + '</form>')
                .appendTo('body').submit().remove();
            setTimeout(function(){
                layer.closeAll('loading');
            }, 3000);
        }
    };
    return self;
})()

